#include <iostream>
#include <cmath>
using namespace std;
int n,ans;
int used[25];
char x;
string s[25];
int num[25][25];
void gqr(int gg,int qr){
	for(int i=1;i<=n;++i){
		if(used[i]&&num[gg][i]){
			--used[i];
			gqr(i,qr+num[gg][i]);
			++used[i];
		}
	}
	ans=max(ans,qr);
}
int main(){
	cin>>n;
	for(int i=1;i<=n;++i)used[i]=2;
	for(int i=1;i<=n;++i)cin>>s[i];
	cin>>x; 
	for(int i=1;i<=n;++i){
		for(int j=1;j<=n;++j){
			if(i==j){
				int mm=0,nn=s[i].size()-1,len;
				string s1="",s2="";
				for(int k=mm;k<=nn;++k){
					s1=s[i][nn-k]+s1;s2=s2+s[i][k];
					if(s1==s2){len=k;break;}
				}
				if(len!=nn)num[i][j]=nn-len;
			}
			else{
				int mm=s[i].size()-1;
				int nn=s[j].size()-1;
				int len=-1;
				string s1="",s2="";
				for(int k=0;k<=min(mm,nn);++k){
					s1=s[i][mm-k]+s1;s2=s2+s[j][k];
					if(s1==s2){len=k;break;}
				}
				if(len==min(mm,nn)||len==-1)continue;
				num[i][j]=nn-len;
			}
			
		}
	}
	for(int i=1;i<=n;++i)if(s[i][0]==x){--used[i];gqr(i,s[i].size());++used[i];}
	cout<<ans<<endl;
	return 0;
}
